package com.finstone.data.spider.eshop.processor;

import com.finstone.data.spider.eshop.service.impl.EShopGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EShopPageProcessor implements PageProcessor {

    @Autowired
    private EShopGoodsService eShopGoodsService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private Site site = Site.me().setCycleRetryTimes(3).setTimeOut(30000);

    public Site getSite() {
        site.addHeader("Referer", "http://www.gec123.com/");
        site.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36");
        return site;
    }

    public void process(Page page) {
        Html html = page.getHtml();
        Selectable selectables = html.$(".le_sel > ul > li");
        List<Map<String, Object>> plist = new ArrayList<Map<String, Object>>();
        for (Selectable item : selectables.nodes()) {
            String title = item.$(".clearfix .le_sel_con1","text").toString();
            if(StringUtils.indexOf(title,"已选条件") > -1){
                continue;
            }
            List<String> title_values = item.$(".le_sel_con2 a", "data-name").all();
            String valueStr = StringUtils.join(title_values, ",");
            Map<String, Object> itemMap = new HashMap<String, Object>();
            itemMap.put("category_id", "1a60e7bf-49e3-493e-8fa3-ad907226d92e");
            itemMap.put("name", title);
            itemMap.put("value", valueStr);
            plist.add(itemMap);
        }


        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Map<String, Object> itemMap = new HashMap<String, Object>();
        String req_url = "http://s.lenovo.com.cn/search/lenovo?key=&innerKey=&filter=&stort=0&category=1a60e7bf-49e3-493e-8fa3-ad907226d92e&page=1&pageSize=24&isStock=false&leid=1.AX6l5Ph8vy0";
        eShopGoodsService.putTask(req_url);
        itemMap.put("id", "1a60e7bf-49e3-493e-8fa3-ad907226d92e");
        itemMap.put("url", page.getUrl().get());
        itemMap.put("name1", "笔记本");
        itemMap.put("name2", "Lenovo笔记本");
        itemMap.put("name3", "Lenovo笔记本");
        list.add(itemMap);
        page.putField("category_id","1a60e7bf-49e3-493e-8fa3-ad907226d92e");
        page.putField("shopCategorty", list);
        page.putField("shopCategortyAttrs", plist);
    }
}
